package com.embrace.CookingForMyWife.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.embrace.CookingForMyWife.commom.R;
import com.embrace.CookingForMyWife.entity.User;
import com.embrace.CookingForMyWife.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 小程序用户登录
    @PostMapping("/login")
    public R<User> login(@RequestBody User user) {
        // 通过 openid 查找用户
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("openid", user.getOpenid());
        User dbUser = userService.getOne(query);

        if (dbUser != null) {
            return R.success(dbUser);
        } else {
            // 注册新用户
            userService.save(user);
            return R.success(user);
        }
    }
}
